package com.maybach.workflow.core.repository;

import com.maybach.workflow.common.model.*;
import com.maybach.workflow.common.query.FlowQuery;
import com.maybach.workflow.common.query.FlowTaskActorQuery;
import com.maybach.workflow.common.query.FlowTaskHisQuery;
import com.maybach.workflow.common.query.FlowTaskQuery;
import com.maybach.workflow.core.manager.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by houenxun on 16/1/20.
 */
public class FlowRepository implements IFlowRepository{
    @Autowired
    protected FlowManager flowManager;
    @Autowired
    protected FlowTaskManager flowTaskManager;
    @Autowired
    protected FlowTaskHisManager flowTaskHisManager;
    @Autowired
    protected FlowActLogManager flowActLogManager;

    @Autowired
    protected FlowTaskActorManager flowTaskActorManager;

    @Override
    public void createTaskActors(List<FlowTaskActorDO> list) {
        for(FlowTaskActorDO taskActorDO: list){
            flowTaskActorManager.insertSelective(taskActorDO);
        }
    }

    @Override
    public FlowDO getFlowById(Long flowId) {
        return flowManager.selectByPrimaryKey(flowId);
    }

    @Override
    public List<FlowTaskDO> getFlowTasksByFlowId(Long flowId) {
        FlowTaskQuery query = new FlowTaskQuery();
        FlowTaskQuery.Criteria criteria = query.or();
        criteria.andFlowIdEqualTo(flowId);

        return flowTaskManager.selectByQuery(query);
    }

    @Override
    public boolean updateFlowStatus(Long flowId, int status) {
        FlowDO flowDO = new FlowDO();
        flowDO.setId(flowId);
        flowDO.setFlowState(status);
        return flowManager.updateByPrimaryKeySelective(flowDO) > 0;

    }

    @Override
    public FlowTaskHisDO getFlowTaskHistByTaskId(Long taskId) {
        return flowTaskHisManager.selectByPrimaryKey(taskId);
    }

    @Override
    public FlowTaskDO getFlowTaskById(Long taskId) {
        return flowTaskManager.selectByPrimaryKey(taskId);
    }

    @Override
    public List<FlowDO> getFlowByBizIdAndType(Long bizId, String flowBizType) {
        FlowQuery query = new FlowQuery();
        FlowQuery.Criteria criteria = query.or();
        criteria.andBizIdEqualTo(bizId);
        criteria.andBizTypeEqualTo(flowBizType);
        return flowManager.selectByQuery(query);
    }

    @Override
    public Long createFlowActLog(FlowActLogDO flowActLogDO) {
        return flowActLogManager.insertSelective(flowActLogDO);
    }

    @Override
    public Long createFlowTask(FlowTaskDO taskDO) {
        return flowTaskManager.insertSelective(taskDO);
    }

    @Override
    public Long createFlowTaskHis(FlowTaskHisDO hisDO) {
        return flowTaskHisManager.insertSelective(hisDO);
    }

    @Override
    public boolean delFlowTaskById(Long taskId) {
        return flowTaskManager.deleteByPrimaryKey(taskId) > 0;
    }

    @Override
    public List<FlowTaskHisDO> getFlowTaskHissByFlowId(Long flowId) {
        FlowTaskHisQuery query = new FlowTaskHisQuery();
        FlowTaskHisQuery.Criteria criteria = query.or();
        criteria.andFlowIdEqualTo(flowId);
        return flowTaskHisManager.selectByQuery(query);
    }

    @Override
    public boolean updateTaskHistState(Long taskHisId, Integer status) {
        FlowTaskHisDO flowTaskHisDO = new FlowTaskHisDO();
        flowTaskHisDO.setId(taskHisId);
        flowTaskHisDO.setTaskState(status);
        return flowTaskHisManager.updateByPrimaryKeySelective(flowTaskHisDO) > 0;
    }

    @Override
    public List<FlowTaskActorDO> getTaskActorsByTaskId(Long taskId) {
        FlowTaskActorQuery query = new FlowTaskActorQuery();
        FlowTaskActorQuery.Criteria criteria = query.or();
        criteria.andTaskIdEqualTo(taskId);
        return flowTaskActorManager.selectByQuery(query);
    }
    @Override
    public boolean clearTaskActorByTaskId(Long taskId){
        FlowTaskActorQuery query = new FlowTaskActorQuery();
        FlowTaskActorQuery.Criteria criteria = query.or();
        criteria.andTaskIdEqualTo(taskId);
        return flowTaskActorManager.deleteByQuery(query) > 0;
    }

    @Override
    public Long createFlow(FlowDO flowDO) {
        return flowManager.insertSelective(flowDO);
    }

    @Deprecated
    @Override
    public FlowTaskDO getTaskByType(Long id, String name, String name1) {
        return null;
    }
    @Deprecated
    @Override
    public List<FlowTaskDO> getFlowTasksByFlowIdAndType(Long id, String name) {
        return null;
    }

    @Deprecated
    @Override
    public FlowTaskDO getFlowTaskWithName(Long id, String actionName) {
        return null;
    }


}
